export const isNaN = (value: number | string | boolean | object | null | undefined): boolean =>
  Number.isNaN(value)
export const isNumber = (value: number | string | boolean | object | null | undefined): boolean =>
  typeof value === 'number' && !isNaN(value)
export const isString = (value: number | string | boolean | object | null | undefined): boolean =>
  typeof value === 'string' && value.trim() !== ''
export const isFunction = (value: unknown): boolean => typeof value === 'function'
export const isObject = (value: unknown): boolean => typeof value === 'object' && value !== null
export const isArray = (value: unknown): boolean => Array.isArray(value)
export const isDate = (value: unknown): boolean => value instanceof Date
export const isUndefined = (value: unknown): boolean => typeof value === 'undefined'
export const isNull = (value: unknown): boolean => value === null
export const isBoolean = (value: boolean | string | number | object | null | undefined): boolean =>
  typeof value === 'boolean'

// 深拷贝
export const deepClone = <T>(obj: T): T => {
  if (obj === null || typeof obj !== 'object') {
    return obj
  }

  // 判断 obj 是数组还是对象，并相应初始化 result
  const result: T = Array.isArray(obj) ? ([] as unknown as T) : ({} as T)

  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      result[key] = deepClone(obj[key])
    }
  }

  return result
}

// 是否是二维数组
export const is2DArray = (arr: unknown): boolean => {
  if (!isArray(arr) || !Array.isArray(arr)) {
    return false
  }
  return (arr as unknown[]).every((item) => isArray(item))
}
